// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class CategoryMapper
    {

        /// <summary />
        static CategoryMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Category> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.CategorySelectAll);

            return Load(commands, dataMappers);
        }

        private Category.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Category.DataTransfer data=new Category.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Category.DataTransfer FillData(Category.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("CategoryID");
            if (!record.IsDBNull(ord))
                data.CategoryID=record.GetInt32(ord);

            ord=record.GetOrdinal("CategoryName");
            if (!record.IsDBNull(ord))
                data.CategoryName=record.GetString(ord);

            ord=record.GetOrdinal("Description");
            if (!record.IsDBNull(ord))
                data.Description=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Category.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CategorySelect,
                    parameterBuilder.GetMarker("CategoryID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CategoryDelete,
                    parameterBuilder.GetMarker("CategoryID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CategoryInsert,
                    parameterBuilder.GetMarker("CategoryName", 1),
                    parameterBuilder.GetMarker("Description", 2)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryName", 1),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Description", 2),
                DbType.String,
                8192,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Description ?? (object)DBNull.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Category.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CategoryUpdate,
                    parameterBuilder.GetMarker("CategoryName", 1),
                    parameterBuilder.GetMarker("Description", 2),
                    parameterBuilder.GetMarker("CategoryID", 3)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryName", 1),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Description", 2),
                DbType.String,
                8192,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Description ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryID", 3),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
